package com.hua.demo.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hua.demo.edu.entity.Chapter;
import com.hua.demo.edu.entity.Video;
import com.hua.demo.edu.entity.chapter.ChapterVo;
import com.hua.demo.edu.entity.chapter.Videovo;
import com.hua.demo.edu.mapper.ChapterMapper;
import com.hua.demo.edu.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hua.demo.edu.service.VideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author huazi
 * @since 2021-11-24
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private VideoService videoService;
    @Override
    public List<ChapterVo> getchaptervideobycourseid(String courserid) {
        QueryWrapper<Chapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id",courserid);
        List<Chapter> list = this.list(chapterQueryWrapper);


        QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id",courserid);
        List<Video> list1 = videoService.list(videoQueryWrapper);
        //遍历章节list进行封装
        ArrayList<ChapterVo> finallist = new ArrayList<>();
        for (Chapter chapter : list) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);
            finallist.add(chapterVo);
            String id = chapter.getId();
            for (Video video : list1) {
                Videovo videovo = new Videovo();
                BeanUtils.copyProperties(video,videovo);
                if(id.equals(video.getChapterId())){
                    chapterVo.getList().add(videovo);
                }

            }
        }

        return finallist;
    }
}
